package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 员工
 *
 * @Author Yang
 * @Date 2023/3/1 17:06
 */
@RestController
@Api(tags = "员工")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpSession session;

    // 员工退出
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        //自动销毁
        session.invalidate();
        return ResultInfo.success();
    }

    //登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee employee) {

        Employee employee1 = employeeService.login(employee.getUsername(), employee.getPassword());

        return ResultInfo.success(employee1);
    }

    //查询所有
    @GetMapping("employee/find")
    public ResultInfo find(String name){
        List<Employee> list = employeeService.find(name);
        return ResultInfo.success(list);
    }

    //新增
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        return ResultInfo.success();
    }

    //id回显
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Employee employee = employeeService.findById(id);
        return ResultInfo.success(employee);
    }

    //修改
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success();
    }
}
























